package com.oa.co.controller;

import com.github.pagehelper.PageInfo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.pojo.OrderRoom;
import com.oa.co.pojo.Purchase;
import com.oa.co.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Lone Ranger
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/api/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;


    // 预约采购
    @RequestMapping("/addPurchase")
    public R addOrder(@RequestBody Purchase purchase) {
        System.out.println(purchase);
        Integer num = purchaseService.addPurchase(purchase);
        if (num == 1) {
            return R.ok().message("预约采购成功");
        }
        return R.error().message("预约采购失败");
    }

    // 修改采购信息
    @RequestMapping("/editPurchase")
    public R editOrder(@RequestBody Purchase purchase) {
        System.out.println(purchase);
        Integer num = purchaseService.modifyPurchase(purchase);
        if (num == 1) {
            return R.ok().message("预约已修改");
        }
        return R.error().message("预约修改失败");
    }

    // 查询所有预约信息
    @RequestMapping("/showAllPurchase")
    public PageVo<Purchase> orderList(Integer page, Integer limit,String purchase_id, String purchase_username, String purchase_dept){

        PageVo<Purchase> pageVo = new PageVo<>();
        //使用 mybatis的分页插件
        PageInfo<Purchase> pageData = purchaseService.getAllPurchases(page, limit, purchase_id, purchase_username, purchase_dept);

        // 把分页查询结果 封装到 layui 的 列表组件支持的数据结构中
        pageVo.setData(pageData.getList());

        pageVo.setCode(0);

        pageVo.setCount(pageData.getTotal());

        pageVo.setMsg("预约会议室的信息");
        return pageVo;
    }

    // 删除单个采购
    @RequestMapping("/deletePurchase")
    public R deleteOrder(String purchase_id){
//        System.out.println(purchase_id);
        Integer num = purchaseService.deletePurchase(purchase_id);
        if (num == 1) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");

    }

    // 删除多个采购预约
    @RequestMapping("/deletePurchaseMulti")
    public R deleteOrderMulti(@RequestBody List<Purchase> list){
        System.out.println(list);
        Integer num = purchaseService.deletePurchaseMulti(list);
        if (num == list.size()) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");

    }



}
